package 题目集.贪心.排序模拟;// package acm.pta10;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;

//https://pintia.cn/problem-sets/1744287237921226752/exam/problems/1744287275661570059?type=7&page=0
public class 秀恩爱分得快 {

    public static void main(String[] args) {
        Reader sc= new Reader();
        int n = sc.nextInt();
        int m = sc.nextInt();
        Set<String>[] sets = new Set[m];
        for (int i = 0; i < sets.length; i++) {
            sets[i]=new HashSet<>();
            int k = sc.nextInt();
            while (k-->0){
                sets[i].add(sc.next());
            }
        }
        String a = sc.next();
        String b = sc.next();
        Node nodeA = new Node(a);
        Node nodeB = new Node(b);
        for (Set<String> set : sets) {
            double score = 1.0/set.size();
            if(set.contains(a)){
                for (String s : set) {
                    nodeA.awarded(s,score);
                }
            }
            if (set.contains(b)){
                for (String s : set) {
                    nodeB.awarded(s,score);
                }
            }
        }
        if (nodeA.isMax(b)&&nodeB.isMax(a)) {
            System.out.println(a+" "+b);
            return;
        }
        nodeA.print();
        nodeB.print();
    }

    static class Node{
        boolean sex;    //f：女，t：男
        double max;
        String id;

        Map<Integer,Double> map = new HashMap<>(); //亲密度关系

        public Node(String id) {
            this.id=id;
            sex= id.charAt(0) != '-';
        }

        /**
         * 对name的亲密度加val
         */
        public void awarded(String name, double val){
            if (sex){   //是否男性
                if (name.charAt(0)!='-')return;
            }else {
                if (name.charAt(0)=='-')return;
            }
            int key = Integer.parseInt(name);
            Double score = map.getOrDefault(key, 0.0);
            score+=val;
            map.put(key,score);
            max=Math.max(max,score);
        }
        public boolean isMax(String key){
            return map.getOrDefault(Integer.parseInt(key),0.0)==max;
        }
        public List<Integer> getMaxs(){
            //女性升序
            List<Integer> res = map.entrySet().parallelStream().filter(e -> e.getValue() == max).map(
                    k -> Math.abs(k.getKey())
            ).sorted(Integer::compareTo).collect(Collectors.toList());
            return res;
        }
        public void print(){
            if (sex)
                getMaxs().forEach(s-> System.out.println(id+" -"+s));
            else
                getMaxs().forEach(s-> System.out.println(id+" "+s));
        }
    }
    static
    class Reader {
        private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer tokenizer = new StringTokenizer("");

        private String innerNextLine() {
            try {
                return reader.readLine();
            } catch (IOException ex) {
                return null;
            }
        }

        public boolean hasNext() {
            while (!tokenizer.hasMoreTokens()) {    //内存中没有
                String nextLine = innerNextLine();
                if (nextLine == null) {     //且没有下一行
                    return false;
                }
                tokenizer = new StringTokenizer(nextLine);  //读入一行
            }
            return true;
        }

        public String nextLine() {
            tokenizer = new StringTokenizer("");    //清空内存中的字符
            return innerNextLine();
        }

        public String next() {
            hasNext();
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble(){
            return Double.parseDouble(next());
        }
//    public BigInteger nextBigInt() {
//        return new BigInteger(next());
//    }
    }

}

